#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int N = 1e5 + 10;

LL n, t;
LL r[N], m[N], a[N], tmp[N];
LL limit;


LL qmul(LL x, LL y, LL p)
{
	LL ans = 0;
	while(y)
	{
		if(y & 1) ans = (ans + x) % p;
		x = (x + x) % p;
		y >>= 1;
	}
	return ans;
}

LL exgcd(LL f, LL g, LL& x, LL& y)
{
	if(g == 0)
	{
		x = 1, y = 0;
		return f;
	}
	LL x0, y0, d;
	d = exgcd(g, f % g, x0, y0);
	x = y0, y = x0 - f / g * y0;
	return d;
}

void init()
{
	limit = 0;
	multiset<LL> se;
	cin >> n >> t;
	for(int i = 1;i <= n;i ++) cin >> r[i];
	for(int i = 1;i <= n;i ++) cin >> m[i];
	for(int i = 1;i <= n;i ++) cin >> tmp[i];
	for(int i = 1;i <= t;i ++)
	{
		LL x; cin >> x;
		se.insert(x);
	}
	for(int i = 1;i <= n;i ++)
	{
		auto it = se.upper_bound(r[i]);
		if(it != se.begin()) it --;
		a[i] = *it;
		se.erase(it);
		se.insert(tmp[i]);

		limit = max(limit, (a[i] + r[i] - 1) / a[i]);
	}
}

LL excrt()
{
	LL M = 1, ret = 0;
	for(int i = 1;i <= n;i ++)
	{
		LL A = qmul(a[i], M, m[i]);
		LL B = m[i], C = r[i] - a[i] * ret;
		C = (C % B + B) % B;

		LL x, y, d;
		d = exgcd(A, B, x, y);
		if(C % d) return -1;
		LL k = B / d;
		x = qmul(x, C / d, k);
		x = (x % k + k) % k;

		ret = ret + x * M;
		M = k * M;

		ret = (ret % M + M) % M;
	}
	if(ret < limit) ret = (limit - ret + M - 1) / M * M + ret;
	return ret;
}

void solve()
{
	init();
	cout << excrt() << endl;
}

int main()
{
	LL T; cin >> T;
	while(T --)
	{
		solve();
	}

	return 0;
}